/*
 * Created on Oct 10, 2005
 *	this is a singleton 
 */
package littlecyberwar.tool;

import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.awt.*;
import java.io.*;
import java.awt.geom.*;

import javax.swing.ImageIcon;

import littlecyberwar.ui.Element;
import littlecyberwar.util.Util;

public class LayeredUnitHandler implements Serializable {

	public static final int _numOfLayers = 5;
	private static boolean layerEnabled[] = new boolean[_numOfLayers];
	private static ArrayList layers[] = new ArrayList[_numOfLayers];
	static {
		for ( int i=0; i < _numOfLayers; i++){
			layers[i] = new ArrayList();
			layerEnabled[i] = false;
		}
		layerEnabled[2] = true;		// only enable the middle layer to start with
	}
	
	private LayeredUnitHandler handler = new LayeredUnitHandler();
	
	private LayeredUnitHandler() {
		super();
		// TODO Auto-generated constructor stub
	}

	public static void addUnitsToLayers( ArrayList units, int layerIndex){
		ArrayList targetArray = layers[layerIndex];
		targetArray.addAll(units);
		// now add the layer into the units itseld
		Integer layer = new Integer(layerIndex);
		for ( int i=0; i < units.size(); i++ ){
			Element el = (Element)units.get(i);
			el.setNewProps(Util._layer, layer);
		}
		return;
	}
	
	public static void moveUnitsBetweenLayers( Element el, int src, int dest) {
		ArrayList srcArray = layers[src];
		srcArray.remove(el);
		ArrayList destArray = layers[dest];
		destArray.add(el);
		el.setNewProps(Util._layer, new Integer(dest));
		return;
	}
	
	// loop through all the layers (from low to high) and start drawing
	//
	public static void paintUnits( Graphics2D g2 ) { 	
		for (int j=0; j < _numOfLayers; j++){
			ArrayList v = layers[j];
			for ( int i=0; i< v.size(); i++ ) {
				  Element e = (Element)v.get(i);
				  /* Old paint a solid rectangle way
				  Shape r = e.getFinalShape();
				  g2.setColor(Color.red);
				  g2.fill(r);
				  */
				  ImageIcon icon = e.getImageIcon();
				  AffineTransform trans = e.getTransform();
				  g2.drawImage(icon.getImage(), trans, null);
			}
		}
		return;
	}
	
	public static Element checkElementSelected( Point p ){
		Element val = null;
		for (int j=0; j < _numOfLayers; j++){
			if ( layerEnabled[j] == true){
				// layer is enabled, so check for selection
				//
				ArrayList v = layers[j];
	
				int size = v.size();
				for (int i=0; i< size; i++) {
				  Element el = (Element)v.get(i);
				  Shape transR = el.getFinalShape();
				  if ( transR.contains(p)) {
					// the mouse click does fall inside of an element's area
					val = el;
								
					// now move the selected element to the last element
					// so will be painted at the top
					//
					v.remove(el);
					v.add(el);
					return ( val );
				  }
				}
			}
		}
		return ( val );
	}
	
	/**
	 * 
	 */
	public static ArrayList checkAreaSelection(Rectangle selectionRectangle) {
		ArrayList results = new ArrayList();
		for (int j=0; j < _numOfLayers; j++){
			if ( layerEnabled[j] == true){
				// layer is enabled, so check for selection
				//
				ArrayList v = layers[j];	
				for (int i=0; i< v.size(); i++) {
				  Element el = (Element)v.get(i);
				  Shape transR = el.getFinalShape();
				  Rectangle2D elementRec = transR.getBounds2D();
				  
				  if ( selectionRectangle.contains(elementRec)) {
					results.add(el);
//					System.out.println(el + " selected");
//					v.remove(el);
//					v.add(el);
				  }
				} // for (int i=0; i< v.size(); i++)
			} //if ( layerEnabled[j] == true)
		}
		return ( results );
	}


	public static void enableLayer( int layerIndex ){
		layerEnabled[layerIndex] = true;
	}
	
	public static void disbaleLayer( int layerIndex ){
		layerEnabled[layerIndex]= false;
	}
	
	/**
	 * This call will automatically add element to the lowest level that's enabled
	 * 
	 * @param el
	 */
	public static boolean addElementToDefault( Element el){
		boolean result = false;
		// first find the lowest enabled layer
		for ( int i=0; i < _numOfLayers; i++ ){
			if ( layerEnabled[i]== true ){
				layers[i].add(el);
				el.setNewProps(Util._layer, new Integer(i));
				result = true;
				break;
			}
		}
		return ( result );
	}
	
	public static void addElementToLayer( Element el, Integer index ){
		layers[index.intValue()].add(el);
	}
	
	/**
	 * 
	 * @param el element to be removed from its current layer
	 */
	public static void removeElement( Element el){
		for (int j=0; j < _numOfLayers; j++){
			if ( layerEnabled[j] == true){
				// layer is enabled, so check for selection
				//
				ArrayList v = layers[j];
				boolean found = v.contains(el);
				if ( found ){
					v.remove(el);
					break;
					
				} else {
					continue;
				}
			}
		}	
		return;
	}
	
	public static boolean isLayerEnabled(int layerIndex){
		return (layerEnabled[layerIndex]);
	}
}
